from django.db import models
from django.contrib.auth.models import User
from DjangoUeditor.models import UEditorField
# Create your models here.

#文章分类
class Fenlei(models.Model):
    name = models.CharField('文章分类', max_length=100)
    index = models.IntegerField(default=999, verbose_name='分类排序')

    class Meta:
        verbose_name = '文章分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

#文章标签
class Biaoqian(models.Model):
    name = models.CharField('文章标签', max_length=100)

    class Meta:
        verbose_name = '文章标签'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name

#推荐位置
class Tui(models.Model):
    name = models.CharField('推荐位', max_length=100)

    class Meta:
        verbose_name = '推荐位'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

#文章
class WenZ(models.Model):
    title = models.CharField('标题', max_length=70)
    zaiyao = models.TextField('摘要', max_length=200, blank=True)
    fenlei = models.ForeignKey(Fenlei, on_delete=models.DO_NOTHING, verbose_name='分类', blank=True, null=True)
    #使用外键，是一对多关系
    biaoqian = models.ManyToManyField(Biaoqian, verbose_name='标签', blank=True)
    #外键关联，多对多
    img = models.ImageField(upload_to='WenZ_img/%Y/%m/%d', verbose_name='文章封面', blank=True,null=True)
    body = UEditorField('内容', width=800, height=500,
                    toolbars="full", imagePath="upimg/", filePath="upfile/",
                    upload_settings={"imageMaxSize": 1204000},
                    settings={}, command=None, blank=True
                    )
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='作者')
    #User是通过django.contrib导入,通过外键把文章和User关联
    views = models.PositiveIntegerField('浏览次数', default=0)
    tui = models.ForeignKey(Tui, on_delete=models.DO_NOTHING, verbose_name='推荐位', blank=True, null=True)
    created_time = models.DateTimeField('发布时间', auto_now_add=True)
    xiugai_time = models.DateTimeField('修改时间', auto_now=True)

    dianzan = models.PositiveIntegerField('点赞数', default=0)
    # diancai = models.PositiveBigIntegerField('点踩数', default=0)

    class Meta:
        verbose_name = '文章'
        verbose_name_plural = '文章'

    def __str__(self):
        return self.title

#幻灯片
class Banner(models.Model):
    text_info = models.CharField('标题', max_length=50, default='')
    img = models.ImageField('幻灯片', upload_to='banner/')
    link_url = models.URLField('图片链接', max_length=100)
    is_active = models.BooleanField('是否激活',default=False)

    class Meta:
        verbose_name = '幻灯片'
        verbose_name_plural = '幻灯片'

    def __str__(self):
        return self.text_info

#友情链接
class Link(models.Model):
    name = models.CharField('链接名称', max_length=20)
    linkurl = models.URLField('链接', max_length=100)

    class Meta:
        verbose_name = '友情链接'
        verbose_name_plural = '友情链接'
    def __str__(self):
        return self.name


